package com.example.equipmentjava.xiaoxinyan.utils;

import javax.net.ssl.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SSLBypassUtil {

    // 创建一个不校验SSL证书的TrustManager
    private static final TrustManager[] trustAllCerts = new TrustManager[]{
            new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
                public void checkClientTrusted(
                        java.security.cert.X509Certificate[] certs, String authType) {
                }
                public void checkServerTrusted(
                        java.security.cert.X509Certificate[] certs, String authType) {
                }
            }
    };

    // 安装所有证书为受信任的TrustManager
    private static void installAllTrustedCertificates() throws Exception {
        final SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
    }

    // 发送POST请求
    public static String sendPost(String urlString, Map<String, Object> params) throws Exception {
        installAllTrustedCertificates();

        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json; utf-8");
        conn.setRequestProperty("Accept", "application/json");

        // 将参数转换为JSON字符串
        String jsonInputString = new org.json.JSONObject(params).toString();

        // 发送POST输出
        try (OutputStream os = conn.getOutputStream()) {
            byte[] input = jsonInputString.getBytes("utf-8");
            os.write(input, 0, input.length);
        }

        // 读取响应
        try (BufferedReader br = new BufferedReader(
                new InputStreamReader(conn.getInputStream(), "utf-8"))) {
            StringBuilder response = new StringBuilder();
            String responseLine;
            while ((responseLine = br.readLine()) != null) {
                response.append(responseLine.trim());
            }
            return response.toString();
        }
    }

    public static void main(String[] args) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("name", "张三");
            params.put("gender", "1");
            params.put("birthday", "1937-04-16");
            params.put("telephone", "13800138000");
            params.put("sn", "DY09001055");
            params.put("address", "上海市浦东新区毕升路339号");

            String response = sendPost("https://unit.iotguard.cn/u/account/75ae925d-d67c-4fdd-9858-812390761e52/create.do", params);
            System.out.println(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
